<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 300 条记录</span>
          <el-button
            @click="drawerHandle('add')"
            icon="el-icon-plus"
            size="small"
            type="primary"
            round
          >
            添加面经
          </el-button>
        </div>
      </template>
    </el-card>

    <el-table :data="list" border style="width: 100%">
      <el-table-column align="center" label="序号" width="50">
        <template slot-scope="{ $index }">
          {{ obj.pageSize * (obj.current - 1) + 1 + $index }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="stem" label="标题">
      </el-table-column>
      <el-table-column align="center" prop="creator" label="作者">
      </el-table-column>
      <el-table-column align="center" prop="likeCount" label="点赞">
      </el-table-column>
      <el-table-column align="center" prop="views" label="浏览量">
      </el-table-column>
      <el-table-column align="center" prop="createdAt" label="更新时间">
      </el-table-column>

      <el-table-column align="center" label="操作">
        <template slot-scope="{ row }">
          <div class="actions">
            <i class="el-icon-view" @click="drawerHandle('preview', row)"></i>
            <i
              class="el-icon-edit-outline"
              @click="drawerHandle('edit', row)"
            ></i>
            <i class="el-icon-delete" @click="del(row.id)"></i>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 枫叶 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 抽屉 -->
    <el-drawer
      :title="titleType"
      :visible.sync="drawer"
      :direction="direction"
      @close="close"
    >
      <div v-if="title === 'preview'" class="article-preview">
        <h5>{{ ruleForm.stem }}</h5>
        <div v-html="ruleForm.content"></div>
      </div>
      <el-form
        v-else
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="80px"
      >
        <el-form-item label="标题" prop="stem">
          <el-input
            v-model="ruleForm.stem"
            placeholder="输入面经标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quillEditor v-model="ruleForm.content"></quillEditor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">确认</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'
import { getArticle, removeArticle, addArticle, updataArticle } from "@/api/article"
export default {
  name: 'article-page',
  created () {
    this.getData()
  },
  components: {
    quillEditor
  },
  methods: {

    handleClick (row) {
      console.log(row);
    },
    async getData () {
      const res = await getArticle(this.obj)
      console.log(res)
      this.list = res.data.rows
      this.total = res.data.total
    },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`);
      this.obj.pageSize = val
      this.getData()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`);
      this.obj.current = val
      this.getData()
    },
    del (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await removeArticle(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    drawerHandle (type, row) {
      if (type == 'preview' || type == 'edit') {
        this.ruleForm = JSON.parse(JSON.stringify(row))
      }
      console.log(this.ruleForm, 9090909)

      this.title = type
      this.drawer = true
    },
    async submit () {
      if (this.title == 'add') {
        try {
          await addArticle(this.ruleForm)
        } catch (error) {

        }
      } else if (this.title == 'edit') {
        try {
          await updataArticle({ id: this.ruleForm.id, content: this.ruleForm.content, stem: this.ruleForm.stem })
        } catch (error) {

        }
      }
      this.$refs.ruleForm.resetFields()
      this.drawer = false
      this.obj.current = 1
      this.getData()
    },
    close () {
      this.$refs.ruleForm?.resetFields();
    }

  },
  computed: {
    titleType () {
      if (this.title == 'add') {
        return '添加'
      } else if (this.title == 'edit') {
        return '编辑'
      } else {
        return '查看'
      }
    }
  },
  data () {
    return {
      list: [],
      obj: {
        current: 1,
        pageSize: 10,
      },
      total: 0,
      drawer: false,
      direction: 'rtl',
      title: '',
      ruleForm: {
        stem: '',
        content: ''
      },
      rules: {
        stem: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
        content: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
i {
  margin: 0 10px;
}
</style>
